package com.boyi.cycling_master.common;

import android.util.Base64;
import android.util.Log;

import com.boyi.cycling_master.application.CyclingApplication;
import com.boyi.cycling_master.bean.LoginResponseBean;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Map;
import java.util.Set;

import okhttp3.RequestBody;

/**
 * @author Lee_Mofeel
 * @version 1.0
 * @date 2017/7/24/024 9:52
 * @Description
 */

public class PostParams {
    /* 包头数据装载*/
    public static JSONObject makePackageHeader() {
        JSONObject json = new JSONObject();
        JSONObject header = new JSONObject();
        LoginResponseBean.DataCollectionBean userInfo = CyclingApplication.getInstance().getUserInfo();
        try {
//            header.put("session", userInfo.getSession());
//            header.put("userId", userInfo.getUserId());
            header.put("version", "1.0.0");
            header.put("OS", "Android");
            json.put("head", header);
        } catch (JSONException e) {
        }
        Log.d("PostDebug header", header.toString());
        return json;
    }

    public static String makeJsonParams(Map<String, String> map) {
        JSONObject jsonObject = new JSONObject();
        try {
            for (Map.Entry<String, String> entry :
                    map.entrySet()) {
                jsonObject.put(entry.getKey(), entry.getValue());
            }
        } catch (JSONException e) {

        }
        Log.d("PostDebug jsonString", jsonObject.toString());
        String enParams = AES128UtilBase64.encryptAES(jsonObject.toString());
        return enParams;
    }

    /**
     *
     * @param map
     * @return
     */
    public static RequestBody makeParamsBody(Map<String, String> map) {
        String content = makeJsonParams(map);
        JSONObject json = new JSONObject();
        try {
            json.put("data", content);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Log.d("PostDebug data", json.toString());
        return RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"), json.toString());
    }

    /**
     * 原版本 包含包头的请求体封装
     * @param map
     * @return
     */
    public static RequestBody makeParamsBodyWithPackageHeader(Map<String, String> map) {
        String content = makeJsonParamsWhitPackageHeader(map);
        JSONObject json = new JSONObject();
        try {
            json.put("data", content);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Log.d("PostDebug data", json.toString());
        return RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"), json.toString());
    }

    public static String makeJsonParamsWhitPackageHeader(Map<String, String> map) {
        JSONObject jsonObject = makePackageHeader();
        try {
            for (Map.Entry<String, String> entry :
                    map.entrySet()) {
                jsonObject.put(entry.getKey(), entry.getValue());
            }
        } catch (JSONException e) {

        }
        Log.d("PostDebug jsonString", jsonObject.toString());
        String enParams = AES128UtilBase64.encryptAES(jsonObject.toString());
        return enParams;
    }

    /**
     * 创建请求Json
     *
     * @param params
     * @return
     */
    public static String makeJson(Map<String, String> params) {
        JSONObject json = new JSONObject();
        try {
            JSONObject paramsJson = new JSONObject();
            json.put("OS", "android");
            json.put("version", "1.0");
            json.put("userId", CyclingApplication.getInstance().getUserInfo() == null ? "" : CyclingApplication.getInstance().getUserInfo().getUserId());
            Set<Map.Entry<String, String>> entrys = params.entrySet();
            for (Map.Entry<String, String> e :
                    entrys) {
                paramsJson.put(e.getKey(), e.getValue());
            }
            String paramsStr = AES128UtilBase64.encryptAES(paramsJson.toString());
            json.put("variable", paramsStr);
            Log.d("PostDebug", json.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return json.toString();
    }

    /**
     * 创建请求体
     *
     * @param params
     * @return
     */
    public static RequestBody makeRequestBody(Map<String, String> params) {
        String content = makeJson(params);
        return RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"), content);
    }

    /**
     * 将文件转换成base64字符串
     * @param filePath
     * @return
     */
    public static String fileToBase64String(String filePath) {
        String data = null;
        File file = new File(filePath);
        FileInputStream inputFile = null;
        try {
            inputFile = new FileInputStream(file);
            byte[] buffer = new byte[(int) file.length()];
            inputFile.read(buffer);
            inputFile.close();
            data = Base64.encodeToString(buffer, Base64.DEFAULT);
            Log.e("Base64", "Base64---->" + data);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return data;
    }

}
